import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86136
 * Date: 2025-04-27
 * Time: 19:47
 */
public class Demo52 {
    public static void main(String[] args) throws Exception {
        Callable<Integer> callable = new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                int result = 0;
                for (int i = 0; i <= 500; i++) {
                    result += i;
                }
                return result;
            }
        };
        //Thread类中没有提供传入callable类对象的构造方法，使用FutureTask对象拿到结果
        FutureTask<Integer> futureTask = new FutureTask<>(callable);
        Thread t = new Thread(futureTask);
        t.start();
        //get方法用来获取callable中call()方法返回的结果
        //如果当前线程一直没有执行完毕，get就会一直阻塞
        System.out.println(futureTask.get());
    }
}
